package com.example.hydropowermanage.controller;

import com.example.hydropowermanage.pojo.Message;
import com.example.hydropowermanage.pojo.User123;
import com.example.hydropowermanage.pojo.UtilityUsage;
import com.example.hydropowermanage.service.LoginService;
import com.example.hydropowermanage.service.MessageService;
import com.example.hydropowermanage.service.lmpl.UtilityUsageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * 登录页面
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private  UtilityUsageServiceImpl waterElectricityService;
    @Autowired
    private MessageService messageService;


    //登录页面
    @RequestMapping("/logon")
    public String logon(){
        return "/logon";
    }

    //超级管理员（列表页面）
    @RequestMapping("/root/index")
    public String index(String username, ModelMap map, User123 user123, Model model){
        user123.setUsername("");
//        List<User123> list = loginService.selectList(user123);
        List<User123> list = loginService.getAllUsers();
//        System.out.println(list);
        model.addAttribute("user123",list);
        map.put("username",username);
        return "/root/index";
    }
    //超级管理员（消息页面）
    @RequestMapping("/root/show msg")
    public String getAllMessages(Model model, String username, ModelMap map){
        List<Message> list = messageService.getUserMessages(username);
        model.addAttribute("messages",list);
        map.put("username",username);
        return "/root/show msg";
    }

    //ajax前后端交互，实现登录跳转
    @GetMapping("/logon1")
    @ResponseBody
    public String logon1(HttpServletRequest request){
        //登陆分成两部分（教职工（包括超级管理员）、普通用户）
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        User123 user = new User123();
        user.setUsername(username);
        List<User123> list = loginService.selectList(user);
        if(list.size()!=0){//当前用户名存在（教职工表中进行判断）
            user.setPwd(pwd);
            List<User123> list1 = loginService.selectList(user);
            if(list1.size()!=0){
                if(list1.get(0).getState()==0){//判断是不是超级管理员
                    return "3";//超级管理员账户（开放所有权限）
                }
                return "1";//账户、密码正确，可以登录！（用户登录）
            }else {
                return "2";//密码错误
            }
        }
        return "0";//当前用户不存在
    }

    //新增页面
    @RequestMapping("/root/insert1")
    public String insert1(Model model){
        return "/root/insert1";
    }


    //新增操作
    @GetMapping("/insertUser1")
    @ResponseBody
    public String insertType1(User123 user123){
        //判断住户信息是否存在
        List<User123> list = loginService.selectList(user123);
        if(list.size()!=0){
            return "1";//失败
        }
        loginService.insertUser(user123);
        return "0";//成功
    }

    //修改
    @RequestMapping("/root/edit")
    public String edit(Model model,int id){
        User123 user123 = loginService.findID(id);
        model.addAttribute("user123",user123);
        return "/root/edit";
    }

    //修改操作
    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateBook(User123 user123){
        Boolean boo = loginService.checkUserUnique(user123);
        if(boo){
            return "1";
        }
        double value = user123.getWaterPrice()*user123.getWaterConsumption()
                +user123.getElectricityPrice()*user123.getPowerConsumption();
        user123.setTotalCost(value);
        waterElectricityService.updatePricesByUserId(user123.getId(),user123.getWaterPrice(),user123.getElectricityPrice());
        loginService.updateUser(user123);
//        System.out.println(user123);
//        System.out.println(value);
//        System.out.println(loginService.findID(user123.getId()));
        return "0";
    }

    //删除
    @RequestMapping("/root/del")
    public String del(Model model,int id,ModelMap map){
        map.put("id",id);
        return "/root/del";
    }

    //删除操作
    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(User123 user123){
        waterElectricityService.removeAllByUserId(user123.getId());
        loginService.deleteUser(String.valueOf(user123.getId()));
        return "0";
    }

    //用户（列表）
    @RequestMapping("/main123")
    public String main123(User123 user123,Model model,ModelMap map,String username){
//        user123.setUsername("");
        List<User123> list = loginService.selectList(user123);
        User123 user = list.get(0);
        List<UtilityUsage> all = waterElectricityService.findAllByUserId(user.getId());

        //电费
        double electricityPrice = list.get(0).getElectricityPrice();
        //水费
        double waterPrice = list.get(0).getWaterPrice();
        double totalWaterUsage = list.get(0).getTotalCost();
        model.addAttribute("waterElectricityInfo",all);
        map.put("username",username);
        map.put("perElec",electricityPrice);
        map.put("perWater",waterPrice);
        map.put("cost",totalWaterUsage);
        return "/main123";
    }



}
